import { Router } from '@angular/router';
import { Component, OnInit, ViewChild } from '@angular/core';
import { STColumn, STComponent, STData } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema, SFSelectWidgetSchema, SFUISchema } from '@delon/form';
import { ModalHelper, _HttpClient } from '@delon/theme';
import { NzModalService } from 'ng-zorro-antd/modal';
import { map } from 'rxjs/operators';
import { OrderManagementService } from '../../services/order-management.service';
import { SearchDrawerService } from '@shared';
import { BasicTableComponent } from 'src/app/routes/commom/components/basic-table/basic-table.component';

@Component({
  selector: 'app-supply-management-complaint',
  templateUrl: './complaint.component.html',
  styleUrls: ['../../../commom/less/commom-table.less', './complaint.component.less']
})
export class OrderManagementComplaintComponent extends BasicTableComponent implements OnInit {
  uiView: SFUISchema = {};
  schemaView: SFSchema = {};
  auditMany = false;
  isVisibleRE = false;
  channelId: any;
  resourceStatus: any;
  selectedMainTabStatus = '2';
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  @ViewChild('sfView', { static: false }) sfView!: SFComponent;
  columns: STColumn[] = [];
  selectedIndex: number = 0;
  isLoading: boolean = false;
  mainTabs = [
    { name: '司机投诉', status: '2' },
    { name: '货主投诉', status: '1' }
  ];
  tabs = [
    {
      name: '全部',
      type: 0
    },
    {
      name: '待处理',
      type: 1
    },
    {
      name: '已处理',
      type: 2
    },
    {
      name: '已撤销',
      type: 3
    },
    {
      name: '已取消',
      type: 4
    },
  ];
  constructor(
    public service: OrderManagementService,
    private modal: NzModalService,
    private router: Router,
    public searchDrawerService: SearchDrawerService
  ) {
    super(searchDrawerService);
    //    console.log(this.selectedIndex);
    //   if (this.selectedIndex === 0) {
    //   this.selectedMainTabStatus = '2';
    // } else if (this.selectedIndex === 1) {
    //   this.selectedMainTabStatus = '1';
    // }
  }

  /**
   * 查询参数
   */
  get reqParams() {
    const a: any = {};
    if (this.resourceStatus) {
      a.complaintStatus = this.resourceStatus;
    }
    if (this.selectedMainTabStatus) {
      a.complainantParty = this.selectedMainTabStatus;
    }
    const params: any = Object.assign({}, this.sf?.value || {});
    delete params._$expand;
    return {
      ...a,
      ...params
    };
  }
  get selectedRows() {
    return this.st?.list.filter(item => item.checked) || [];
  }
  ngOnInit(): void {
    this.initSF();
    this.initST();
    this.initSTAudit();
  }

  /**
   * 初始化查询表单
   */
  initSF() {
    this.schema = {
      properties: {
        _$expand: { type: 'boolean', ui: { hidden: true } },
        complaintCode: {
          type: 'string',
          title: '投诉单号'
        },
        wayBillCode: {
          type: 'string',
          title: '运单号',
          ui: {}
        },
        drvComplaintCause: {
          title: '投诉原因',
          type: 'string',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'drvcomplaint:cause' },
            hidden: this.selectedMainTabStatus == '1',
            containsAllLabel: true
          } as SFSelectWidgetSchema
        },
        shpComplaintCause: {
          title: '投诉原因',
          type: 'string',
          ui: {
            widget: 'dict-select',
            params: { dictKey: 'drvcomplaint:cause' },
            hidden: this.selectedMainTabStatus == '2',
            containsAllLabel: true
          } as SFSelectWidgetSchema
        },
        complainantTime: {
          type: 'string',

          title: '投诉时间',
          ui: {
            widget: 'sl-from-to',
            type: 'date',
            format: 'yyyy-MM-dd',
          } as SFDateWidgetSchema
        }
      }
    };
  }

  /**
   * 初始化数据列表
   */
  initST() {
    this.columns = [
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
        } 
      },
      {
        title: '投诉单号',
        width: '200px',
        fixed: 'left',
        className: 'text-center',
        render: 'complaintCode'
      },
      {
        title: '运单号',
        width: '200px',
        className: 'text-center',
        index: 'wayBillCode'
      },
      {
        title: '投诉时间',
        width: '170px',
        index: 'complainantTime',
        className: 'text-center'
      },
      { title: '托运方', index: 'shipperAppUserName', width: '200px', className: 'text-center' },
      { title: '司机', index: 'driverName', width: '120px', className: 'text-center' },
      // {
      //   title: '投诉原因',
      //   className: 'text-center',
      //   width: '170px',
      //   render: 'complaintCauseLabel'
      // },
      {
        title: '投诉原因',
        iif: value => this.selectedMainTabStatus == '1',
        index: 'shpComplaintCauseLabel',
        className: 'text-center',
        width: '170px'
      },
      {
        title: '投诉原因',
        iif: value => this.selectedMainTabStatus == '2',
        index: 'drvComplaintCauseLabel',
        className: 'text-center',
        width: '170px'
      },
      {
        title: '投诉状态',
        className: 'text-center',
        width: '120px',
        index: 'complaintStatusLabel'
      },
      {
        title: '处理人',
        className: 'text-center',
        width: '200px',
        index: 'handlerName'
      },
      {
        title: '处理时间',
        className: 'text-center',
        width: '200px',
        index: 'handleTime'
      },
      {
        title: '处理结果',
        className: 'text-center',
        width: '170px',
        index: 'handleResult'
      },
      {
        title: '投诉方',
        className: 'text-center',
        width: '120px',
        index: 'complainantPartyLabel'
      },
      {
        title: '投诉人',
        className: 'text-center',
        width: '120px',
        index: 'complainantName'
      },
      {
        title: '操作',
        fixed: 'right',
        width: '100px',
        className: 'text-center',
        buttons: [
          // {
          //   text: '处理',
          //   click: (_record) => this.viewEvaluate(_record),
          //   iif: (item) => item.complaintStatus == 1
          // },
          {
            text: '查看',
            click: _record => this.view(_record),
            acl: { ability: ['ORDER-COMPLAINT-view'] }
          }
        ]
      }
    ];
  }
  initSTAudit() {
    this.schemaView = {
      properties: {
        handleResult: {
          title: '处理结果',
          type: 'string',
          maxLength: 50,
          ui: {
            placeholder: '最多不超过50字',
            widget: 'textarea',
            autosize: { minRows: 3, maxRows: 6 }
          }
        }
      },
      required: ['handleResult']
    };
    this.uiView = { '*': { spanLabelFixed: 110, grid: { span: 24 } } };
  }
  /**
   * 查询字段个数
   */
  get queryFieldCount(): number {
    return Object.keys(this.schema?.properties || {}).length;
  }
  tabChange(item: any) {}
  selectChange(e: number) {
    this.resourceStatus = e;
    this.initST();
    setTimeout(() => {
      this.st.load();
    }, 500);
  }
  /**
   * 切换投诉与被投诉tab
   */
  selectMainTab(e: any) {
    this.selectedMainTabStatus = e?.status;
    this.resourceStatus = '';
    this.initST();
    this.initSF();
    setTimeout(() => {
      this.st.load(1);
    });
  }

  /**
   * 导入货源
   */
  importGoodsSource() {}
  audit(item: any) {}

  /*
  * 审核关闭弹窗
  view: 1
  浮动费用： 0
  查看评价： 3
  */
  handleCancel(type: string) {
    const paramsa = {
      ...this.sfView.value,
      handleStatus: 0,
      id: this.channelId
    };
    this.service.request(this.service.$api_get_dealWithComplaint, paramsa).subscribe((res: any) => {
      if (res) {
        this.service.msgSrv.success('已拒绝！');
        this.isVisibleRE = false;
        this.st.reload(1);
      } else {
        this.service.msgSrv.error(res?.msg);
      }
    });
    this.isVisibleRE = false;
  }
  Cancel() {
    this.isVisibleRE = false;
  }
  handleCancel2() {
    const paramsa = {
      id: this.channelId
    };
    this.service.request(this.service.$api_get_canelComplaint, paramsa).subscribe((res: any) => {
      if (res) {
        this.service.msgSrv.success('已拒绝！');
        this.isVisibleRE = false;
        this.st.reload(1);
      } else {
        this.service.msgSrv.error(res?.msg);
      }
    });
    this.isVisibleRE = false;
  }
  /**
   * 审核通过按钮
   */
  handleOK() {
    const paramsa = {
      ...this.sfView.value,
      handleStatus: 1,
      id: this.channelId
    };
    this.service.request(this.service.$api_get_dealWithComplaint, paramsa).subscribe((res: any) => {
      if (res) {
        this.service.msgSrv.success('已通过！');
        this.isVisibleRE = false;
        this.st.reload(1);
      } else {
        this.service.msgSrv.error(res?.msg);
      }
    });
  }
  /**
   *查看评价
   */
  viewEvaluate(item: any) {
    this.isVisibleRE = true;
    this.channelId = item.id;
  }
  view(value: any) {
    this.router.navigate(['/order-management/complaint-detail/' + value.id], {
      queryParams: {
        detail: JSON.stringify(value),
        sts: this.selectedMainTabStatus
      }
    });
  }
  // 导出
  exprot() {
    this.service.exportStart({ ...this.reqParams, pageSize: -1 }, this.service.$api_asyncExportComplaintListDrv);
  }
}
